package com.zhang.boot.web.config;

import com.rabbitmq.client.Channel;
import com.zhang.boot.core.vo.rabbit.RabbitMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableRabbit
@Slf4j
public class RabbitConfig {

    public final static String ORDER_DELAY_QUEUE = "order.delay.queue";

    public final static String ORDER_RELEASE_QUEUE = "order.release.queue";

    public final static String ORDER_EVENT_EXCHANGE = "order.event.exchange";

    public final static String ORDER_RELEASE_ORDER = "order.release.order";

    public final static String ORDER_CREATE_ORDER = "order.create.order";


    /**
     * rabbit序列化配置
     *
     * @return
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public Queue orderDelayQueue() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", ORDER_EVENT_EXCHANGE);
        arguments.put("x-dead-letter-routing-key", ORDER_RELEASE_ORDER);
        arguments.put("x-message-ttl", 60000);
        //String name, boolean durable, boolean exclusive, boolean autoDelete,
        //			@Nullable Map<String, Object> arguments
        Queue queue = new Queue(ORDER_DELAY_QUEUE, true, false, false, arguments);
        return queue;
    }

    @Bean
    public Queue orderReleaseQueue() {
        //String name, boolean durable, boolean exclusive, boolean autoDelete,
        //			@Nullable Map<String, Object> arguments
        Queue queue = new Queue(ORDER_RELEASE_QUEUE, true, false, false);
        return queue;
    }

    @Bean
    public Exchange orderEventChange() {
        //String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
        TopicExchange topicExchange = new TopicExchange(ORDER_EVENT_EXCHANGE, true, false);
        return topicExchange;
    }

    @Bean
    public Binding orderCreateBinding() {
        //String destination, DestinationType destinationType, String exchange, String routingKey,
        //			@Nullable Map<String, Object> arguments
        Binding orderCreateBinding = new Binding(ORDER_DELAY_QUEUE, Binding.DestinationType.QUEUE, ORDER_EVENT_EXCHANGE, ORDER_CREATE_ORDER, null);
        return orderCreateBinding;
    }

    @Bean
    public Binding orderReleaseBinding() {
        Binding orderCreateBinding = new Binding(ORDER_RELEASE_QUEUE, Binding.DestinationType.QUEUE, ORDER_EVENT_EXCHANGE, ORDER_RELEASE_ORDER, null);
        return orderCreateBinding;
    }

    @RabbitListener(queues = ORDER_RELEASE_QUEUE)
    public void listener(Message message, Channel channel, RabbitMessage rabbitMessage) {
        log.info("收到消息:{},消息类型:{},消息内容:{}", rabbitMessage.getMessageId(), rabbitMessage.getMessageType(), rabbitMessage.getMessageContent());
        MessageProperties messageProperties = message.getMessageProperties();
        try {
            channel.basicAck(messageProperties.getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
